package com.industrial.premu.controller;

import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.system.api.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.premu.domain.PreMuDifferentiationPolicy;
import com.industrial.premu.service.IPreMuDifferentiationPolicyService;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.page.TableDataInfo;

/**
 * 差别化政策管理Controller
 *
 * @author SangGuangCan
 * @date 2024-02-27
 */
@RestController
@RequestMapping("/policy")
public class PreMuDifferentiationPolicyController extends BaseController
{
    @Autowired
    private IPreMuDifferentiationPolicyService preMuDifferentiationPolicyService;

    /**
     * 查询差别化政策管理列表
     */
    @RequiresPermissions("policy:policy:list")
    @GetMapping("/list")
    public TableDataInfo list(PreMuDifferentiationPolicy params)
    {

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
            if(StringUtils.isBlank(params.getReleaseCity())){
                params.setReleaseCounty(sysUser.getDistrict());
            }else{
                params.setReleaseCounty(params.getReleaseCity());
                params.setReleaseCity(null);
            }
        }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
            if(StringUtils.isBlank(params.getReleaseCity())){
                params.setReleaseCity(sysUser.getCity());
            }else{
                if(StringUtils.endsWith(params.getReleaseCity(),"0000")){
                    params.setReleaseProvince(params.getReleaseCity());
                    params.setReleaseCity(null);
                }else if(StringUtils.endsWith(params.getReleaseCity(),"00")){
                    params.setReleaseCity(params.getReleaseCity());
                }else{
                    params.setReleaseCounty(params.getReleaseCity());
                    params.setReleaseCity(null);
                }
            }
        }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
            if(StringUtils.isBlank(params.getReleaseCity())){
                params.setReleaseProvince(sysUser.getProvince());
            }else{
                if(StringUtils.endsWith(params.getReleaseCity(),"0000")){
                    params.setReleaseProvince(params.getReleaseCity());
                    params.setReleaseCity(null);
                }else if(StringUtils.endsWith(params.getReleaseCity(),"00")){
                    params.setReleaseCity(params.getReleaseCity());
                }else{
                    params.setReleaseCounty(params.getReleaseCity());
                    params.setReleaseCity(null);
                }
            }
        }

        startPage();
        List<PreMuDifferentiationPolicy> list = preMuDifferentiationPolicyService.selectPreMuDifferentiationPolicyList(params);
        return getDataTable(list);
    }

    /**
     * 导出差别化政策管理列表
     */
    @RequiresPermissions("policy:policy:export")
    @Log(title = "差别化政策管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreMuDifferentiationPolicy preMuDifferentiationPolicy)
    {
        List<PreMuDifferentiationPolicy> list = preMuDifferentiationPolicyService.selectPreMuDifferentiationPolicyList(preMuDifferentiationPolicy);
        ExcelUtil<PreMuDifferentiationPolicy> util = new ExcelUtil<PreMuDifferentiationPolicy>(PreMuDifferentiationPolicy.class);
        util.exportExcel(response, list, "差别化政策管理数据");
    }

    /**
     * 获取差别化政策管理详细信息
     */
    @RequiresPermissions("policy:policy:query")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(preMuDifferentiationPolicyService.selectPreMuDifferentiationPolicyByBaseId(baseId));
    }

    /**
     * 新增差别化政策管理
     */
    @RequiresPermissions("policy:policy:add")
    @Log(title = "差别化政策管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PreMuDifferentiationPolicy preMuDifferentiationPolicy)
    {
        return toAjax(preMuDifferentiationPolicyService.insertPreMuDifferentiationPolicy(preMuDifferentiationPolicy));
    }

    /**
     * 修改差别化政策管理
     */
    @RequiresPermissions("policy:policy:edit")
    @Log(title = "差别化政策管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PreMuDifferentiationPolicy preMuDifferentiationPolicy)
    {
        return toAjax(preMuDifferentiationPolicyService.updatePreMuDifferentiationPolicy(preMuDifferentiationPolicy));
    }

    /**
     * 删除差别化政策管理
     */
    @RequiresPermissions("policy:policy:remove")
    @Log(title = "差别化政策管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(preMuDifferentiationPolicyService.deletePreMuDifferentiationPolicyByBaseIds(baseIds));
    }
}
